<?php
/**
 * Template Name: Submit a Listing
 *
 * @package WordPress
 * @subpackage Master Theme
 */
 
get_header(); ?>

    <div id="container">
        <div id="content" role="main">
            
            <?php
				if ( !is_user_logged_in() ) : 
					echo '<p class="page-title">Submit a Listing</p>';
					echo 'Please <a href="' . wp_login_url( curPageURL() ) . '">Log in</a> to Submit a Listing...';
				else:
//				
				// GET THE SSA AND CURRENT USER IFO 
					$url_ssaID = $_GET['ssaID'];
					$meta_ssaID = get_user_meta( get_current_user_id(), 'preferredSSA', true );
					// either set a new perferred SSA or grab current preferred or send user to SSA directory
					if ( !empty($url_ssaID) ) { update_user_meta( get_current_user_id(), 'preferredSSA', $_GET['ssaID'] ); $ssaID = $_GET['ssaID']; } elseif ( !empty($meta_ssaID) ) { $ssaID = $meta_ssaID; } else { wp_redirect( site_url( bp_get_members_root_slug() ) ); }
					$ssa = get_user_by('id', $ssaID);
					global $current_user;
			?>
            
            <p class="page-title">Submit your Short Sale Info to <?php echo $ssa->first_name . ' ' . $ssa->last_name; ?></p>
            
            <p>(*) indicates required field</p>
            
            <?php
				// IF A SUBMISSION
				if ( isset($_POST['ssSubmit']) ) { 				
					
					// GATHER OUR INFO
						$errors = array();
						if ( $_POST['apEmail'] != '' ) { $apEmail = $_POST['apEmail']; } else { $errors[] = 'Please Provide Your Email'; }
						if ( $_POST['apPhone'] != '' ) { $apPhone = $_POST['apPhone']; } else { $errors[] = 'Please Provide Your Phone Number.'; }
						if ( $_POST['ListingAddress'] != '' ) { $ListingAddress = $_POST['ListingAddress']; } else { $errors[] = 'Please Provide Listing Address'; }
						if ( $_POST['ListingPrice'] != '' ) { $ListingPrice = $_POST['ListingPrice']; } else { $errors[] = 'Please Provide Listing Price'; }
						if ( $_POST['ListingDate'] != '' ) { $ListingDate = $_POST['ListingDate']; } else { $errors[] = 'Please Provide Listing Date'; }
						if ( $_POST['ListingExpirationDate'] != '' ) { $ListingExpirationDate = $_POST['ListingExpirationDate']; } else { $errors[] = 'Please Provide Listing Expiration Date'; }
						$EquatorUserName = $_POST['EquatorUserName'];
						$EquatorPassword = $_POST['EquatorPassword'];
						$titleCompany = $_POST['titleCompany'];
						$titleCoContact = $_POST['titleCoContact'];
						$titleCoPhone = $_POST['titleCoPhone'];
						$titleCoEmail = $_POST['titleCoEmail'];
						if ( $_POST['brokerName'] != '' ) { $brokerName = $_POST['brokerName']; } else { $errors[] = 'Please Provide Brokerage Name '; }
					
					if ( empty($errors) ) { // SUCCESS - NO ERRORS
							
						// CREATE A POST OUT OF LISTING FOR REFERENCE
							$postInfo = array(
								'ping_status'    => 'closed',
								'post_author'    => $ssaID,
								'post_status'    => 'publish',
								'post_title'     => $ListingAddress,
								'post_type'      => 'post'
							); 
							$postID = wp_insert_post($postInfo);
							add_post_meta( $postID, 'apID', $current_user->ID );
							add_post_meta( $postID, 'apEmail', $apEmail );
							add_post_meta( $postID, 'apPhone', $apPhone );
							add_post_meta( $postID, 'ListingAddress', $ListingAddress );
							add_post_meta( $postID, 'ListingPrice', $ListingPrice );
							add_post_meta( $postID, 'ListingDate', $ListingDate );
							add_post_meta( $postID, 'ListingExpirationDate', $ListingExpirationDate );
							add_post_meta( $postID, 'EquatorUserName', $EquatorUserName );
							add_post_meta( $postID, 'EquatorPassword', $EquatorPassword );
							add_post_meta( $postID, 'titleCompany', $titleCompany );
							add_post_meta( $postID, 'titleCoContact', $titleCoContact );
							add_post_meta( $postID, 'titleCoPhone', $titleCoPhone );
							add_post_meta( $postID, 'titleCoEmail', $titleCoEmail );
							add_post_meta( $postID, 'brokerName', $brokerName );
							// THE FOLLOWING META ARE DEFAULT VALUES WE NNED TO HAVE PRESENT FOR THE LISTING DASHBOARD TO WORK AND A FEW OTHER REASONS
							add_post_meta( $postID, 'apDisplayName', $current_user->display_name );
							add_post_meta( $postID, 'accessKey', md5(rand(1000000, 1000000000)) );
							add_post_meta( $postID, 'listingStatus', 'Listing submitted' );
							add_post_meta( $postID, 'listingRating', '0' );
							add_post_meta( $postID, 'reqDocsMissing', true );
							add_post_meta( $postID, 'documentTracking', 'autoTracking' );

						// EMAIL TO AP
							$message1 = 'Thank you for your submission. ' . $ssa->first_name . ' ' . $ssa->last_name . ' will be in contact with you as soon as possible. Incase you have not yet submitted all necessary documents, you can get instructions here: ' . site_url('submit-documents') . '?pid=' . $postID;
							$headers1[] = 'From: [trudocs] trudocs.com';
							wp_mail( $apEmail, 'Listing Confirmation for ' . $ListingAddress, $message1, $headers1 );
							
						// EMAIL TO SSA
							$message2 = "Listing Submission Info: \r\r" . 
							"Name: " . $current_user->user_firstname . " " . $current_user->user_lastname . " \r" . 
							"Email: "  . $apEmail . " \r" . 
							"Phone: "  . $apPhone . " \r" . 
							"Listing Address: "  . $ListingAddress . " \r" . 
							"Listing Price: "  . $ListingPrice . " \r" . 
							"Listing Date: "  . $ListingDate . " \r" . 
							"Listing Expiration Date: "  . $ListingExpirationDate . " \r" . 
							"Equator Username: "  . $EquatorUserName . " \r" . 
							"Equator Password: "  . $EquatorPassword . " \r" . 
							"Title Company Name: "  . $titleCompany . " \r" . 
							"Title Company Contact: "  . $titleCoContact . " \r" . 
							"Title Company Phone: "  . $titleCoPhone . " \r" . 
							"Title Company Email: "  . $titleCoEmail . " \r";
							"Brokerage Name: "  . $brokerName . " \r" . 
							$headers2[] = 'From: [trudocs] trudocs.com';
							$headers1[] = 'Cc: submissions@trudocs.com';
							wp_mail( $ssa->user_email, 'New Listing from ' . $current_user->user_firstname . ' ' . $current_user->user_lastname . ' - ' . $ListingAddress, $message2, $headers2 );
	
						// MOVE USER TO NEXT STEP
							wp_redirect('submit-documents?pid='.$postID );
						
					} else { // FAILURE - OUTPUT ERRORS
						echo '<div class="error">Incomplete Listing Information:</div>';
						echo '<ul>';
							foreach ( $errors as $error ) {
								echo '<li style="color:red;">' . $error . '...</li>';
							}
						echo '</ul>';
					}
				}
            ?>
            
            <form action="" method="post" name="submit-s-s" id="submit-ss">
            	
                <h3>Short Sale Assistant</h3>
                    
                    <label>Name</label>
                    <input type="text" disabled="disabled" value="<?php echo $ssa->first_name . ' ' . $ssa->last_name; ?>" /><br />
                    
                <h3>Your Contact Information*</h3>
                    
                    <label for="apEmail">Email*</label>
                    <input name="apEmail" type="text" value="<?php if ( isset($_POST['apEmail']) ) { echo $_POST['apEmail']; } else { echo $current_user->user_email; } ?>" /><br />
                    
                    <label for="apPhone">Phone*</label>
                    <input name="apPhone" type="text" value="<?php if ( isset($_POST['apPhone']) ) { echo $_POST['apPhone']; } ?>" /><br />
        
                <h3>Listing Information*</h3>
                
                    <label for="ListingAddress">Listing Address*</label>
                    <input name="ListingAddress" type="text" value="<?php if ( isset($_POST['ListingAddress']) ) { echo $_POST['ListingAddress']; } ?>" /><br />
    
                    <label for="ListingPrice">Listing Price*</label>
                    <input name="ListingPrice" type="text" value="<?php if ( isset($_POST['ListingPrice']) ) { echo $_POST['ListingPrice']; } ?>" /><br />
    
                    <label for="ListingDate">Listing Date*</label>
                    <input name="ListingDate" type="text" value="<?php if ( isset($_POST['ListingDate']) ) { echo $_POST['ListingDate']; } ?>" /><br />
    
                    <label for="ListingExpirationDate">Listing Expiration Date*</label>
                    <input name="ListingExpirationDate" type="text" value="<?php if ( isset($_POST['ListingExpirationDate']) ) { echo $_POST['ListingExpirationDate']; } ?>" /><br />
    
                <h3>Equator Information (Optional)</h3>
                
                    <label for="EquatorUserName">Equator User Name</label>
                    <input name="EquatorUserName" type="text" value="<?php if ( isset($_POST['EquatorUserName']) ) { echo $_POST['EquatorUserName']; } ?>" /><br />
    
                    <label for="EquatorPassword">Equator Password</label>
                    <input name="EquatorPassword" type="text" value="<?php if ( isset($_POST['EquatorPassword']) ) { echo $_POST['EquatorPassword']; } ?>" /><br />
    
                <h3>Title Company Information (Optional)</h3>
                
                    <label for="titleCompany">Name</label>
                    <input name="titleCompany" type="text" value="<?php if ( isset($_POST['titleCompany']) ) { echo $_POST['titleCompany']; } ?>" /><br />
    
                    <label for="titleCoContact">Contact</label>
                    <input name="titleCoContact" type="text" value="<?php if ( isset($_POST['titleCoContact']) ) { echo $_POST['titleCoContact']; } ?>" /><br />
    
                    <label for="titleCoPhone">Phone</label>
                    <input name="titleCoPhone" type="text" value="<?php if ( isset($_POST['titleCoPhone']) ) { echo $_POST['titleCoPhone']; } ?>" /><br />
    
                    <label for="titleCoEmail">Email</label>
                    <input name="titleCoEmail" type="text" value="<?php if ( isset($_POST['titleCoEmail']) ) { echo $_POST['titleCoEmail']; } ?>" /><br />
    
                <h3>Brokerage Information*</h3>
                
                    <label for="brokerName">Name*</label>
                    <input name="brokerName" type="text" value="<?php if ( isset($_POST['brokerName']) ) { echo $_POST['brokerName']; } ?>" /><br />
    
                <input id="ssSubmit" name="ssSubmit" type="submit" value="Submit Short Sale" />
            </form>
            
            <?php endif; ?>
            
        </div><!-- #content -->
    </div><!-- #container -->
        


    <div style="clear:both;"></div>

<?php get_footer(); ?>